import { useAppStore } from '@/store'
import type { RunResult } from 'sqlite3'
import { IPC } from 'root/common/constants'
import Vrouter from "@/router"
import { reactive } from 'vue'

// 获取窗体宽度
export const open_win = async(url:any,w:any,h:any,type:any) => {
  await window.ipcRenderer.invoke(IPC.ELECTRON_OPENWIN,url,w,h,type)
}

export const setWindows = async (e:String) => {
  await window.ipcRenderer.invoke(IPC.ELECTRON_WINDOWS, e)
}


export const sqlget = async (sql:any) => {
    try {
        const result: RunResult = await window.ipcRenderer.invoke(IPC.SQL_EXEC, sql)
        return result
    } catch (error: any) {
        return error.message
    }
}

export const goto = (item:any) => {
    const router = Vrouter;
    router.push(
        {
            path:item.path,
            query:{
                id:item.id,
                type:item.type
            }
    });
}

export const on_opendir  = async() => {
    await window.ipcRenderer.invoke(IPC.ELECTRON_OPENDIR)
}

export const reactive_darkThemeOverrides = (primary:any) => {
    const darkThemeOverrides =  reactive({
        common: {
            primaryColor: primary,
            primaryColorHover: primary,
            primaryColorPressed: primary,
            primaryColorSuppl: primary,
            successColor: primary,
            successColorHover: primary,
            successColorPressed: primary,
            successColorSuppl: primary,
            cardColor:'transparent',
            bodyColor:'transparent',
            iconColor:"#fff",
            iconColorDisabled:"#fff",
          },Tabs:{
            tabTextColorActiveBar :"#fff",
            tabTextColorHoverBar :"#fff",
            tabColorSegment: primary,
            tabTextColorActiveSegment:"#fff"
          },Card:{
            // color:"rgba(255,255,255,0.03)",
          },Button:{
            textColorPrimary:"#fff"
          },BackTop:{
            width:"auto"
          },Drawer:{
            color:'#202020',
            bodyPadding:"0"
          }
      });
    return  darkThemeOverrides; 
}

export const reactive_lightThemeOverrides = (primary:any) => {
    const lightThemeOverrides =  reactive({
        common: {
            primaryColor: primary,
            primaryColorHover: primary,
            primaryColorPressed: primary,
            primaryColorSuppl: primary,
            successColor: primary,
            successColorHover: primary,
            successColorPressed: primary,
            successColorSuppl: primary,
            cardColor:'transparent',
            bodyColor:'transparent',
            iconColor:"#000",
            iconColorDisabled:"#000",  
          },Tabs:{
            tabTextColorBar:"#000",
            tabTextColorActiveBar :"#000",
            tabTextColorHoverBar :"#000",
            tabColorSegment: primary,
            tabTextColorActiveSegment:"#fff",
            colorSegment:"rgba(255,255,255,0.3)"
          },Card:{
            color:"rgba(0,0,0,0.1)",
          },BackTop:{
            width:"auto"
          },Drawer:{
            color:'#fff',
            bodyPadding:"0"
          },Tag:{
            color:"rgba(255,255,255,0.3)"
          },Input:{
            color:"rgba(255,255,255,0.3)",
            placeholderColor:"#000"
          }
      });
    return  lightThemeOverrides; 
}

export const  time_format=(time:any) => {
  let ret ="";
  let h = 0;
  let m = 0;
  let s = 0;
  if(time>3600){
    h=Math.floor(time/3600)
    ret = h+":";
  }
  m = Math.floor((time - h*3600)/60)
  if(m<10){
    ret = ret+"0"+m+":";
  }else{
    ret = ret+""+m+":";
  }
  s=Math.floor(time-h*3600-m*60)
  if(s<10){
    ret = ret+"0"+s+"";
  }else{
    ret = ret+""+s+"";
  }
  return ret
}

export const parentPath = (path: string): string => {
  const segments = path.split('/').filter(segment => segment !== ''); // 将路径拆分为数组，并过滤掉空段
  if(segments.length){
    return `/${segments[0]}`;
  }else{
    return `/`;
  }
}

export const reset_theme = async () => {
  const store = useAppStore()
  let theme = {
    dark:true,
    menu_left:false,
    primary:"#D3162C",
    background:"",
    esc_btn:true,
  }
  store.setthems(theme)
}
